package com.example.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.domain.ResponseResult;
import com.example.domain.entity.Comment;
import com.example.exception.FloraException;
import com.example.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;

    /** 查看某篇文章或日记（type）下的根评论 */
    @GetMapping("/getRootComment")
    public IPage<Comment> getCommentList(
            int type, Long articleId,
            @RequestParam(defaultValue = "0",required = false) int pageNum,
            @RequestParam(defaultValue = "5",required = false) int pageSize){
        return commentService.getRootComment(type,articleId,pageNum,pageSize);
    }

    /** 点击展开按钮，查看根评论下的子评论 */
    @GetMapping("/getChildrenComment")
    public IPage<Comment> getChildrenComment(Long commentId,
            @RequestParam(defaultValue = "0",required = false) int pageNum,
            @RequestParam(defaultValue = "5",required = false) int pageSize){
        return commentService.getChildrenComment(commentId,pageNum,pageSize);
    }

    /** 发表评论 */
    @PostMapping("/addComment")
    public Comment addComment(@RequestBody Comment comment) throws FloraException {
        return commentService.addComment(comment);
    }

    /** 删除评论，父评论被删除，对应的子评论也被删除 */
    @DeleteMapping("/deleteComment")
    public void deleteComment(Long commentId) throws FloraException {
        commentService.deleteComment(commentId);
    }


}
